package com.edushare.common.vo;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.edushare.common.dao.BatchDAO;
import com.edushare.common.dao.CourseInstancesDAO;
import com.edushare.common.dao.DepartmentsDAO;
import com.edushare.common.dao.GroupsDAO;
import com.edushare.common.dao.UniversitiesDAO;
import com.edushare.common.dao.UserPollDAO;
import com.edushare.common.orm.ORMInterface;
import com.edushare.common.vo.auto._Poll;

public class Poll extends _Poll implements DateComparision {
	public static final int ANONYMOUS_POLL = 10;
	public static final int NAMED_POLL = 20;

	public static final int POLL_RESULT_OPEN = 10;
	public static final int POLL_RESULT_CLOSED = 20;

	public static final int POLL_STATUS_DRAFT = 10;
	public static final int POLL_STATUS_POLLING = 20;
	public static final int POLL_STATUS_CLOSED = 30;

	public static final int POLL_UNIV = 10;
	public static final int POLL_DEPT = 20;
	public static final int POLL_COURSE = 30;
	public static final int POLL_GROUP = 40;
	public static final int POLL_BATCH = 50;
	public static final int POLL_INDI = 60;

	public static final int POLL_ADMINS = 10;
	public static final int POLL_FACULTY = 20;
	public static final int POLL_TA = 30;
	public static final int POLL_STUDENTS = 40;
	public static final int POLL_MULTIPLE_AUDI = 50;
	public static final int POLL_MEMBERS = 60;

	public String getPollAudi(ORMInterface orm) {
		if(getPollGroupType() == null)
			return "";
		return "A poll for " + getSubAudi(orm) + getAudi(orm);
	}

	private String getSubAudi(ORMInterface orm) {
		int sub = getPollSubgroup();
		switch (sub) {
		case POLL_ADMINS:
			return "administrators of ";
		case POLL_FACULTY:
			return "faculty of ";
		case POLL_TA:
			return "teaching assistants of ";
		case POLL_STUDENTS:
			return "students of ";
		case POLL_MEMBERS:
			return "members of ";
		case POLL_MULTIPLE_AUDI:
			return new UserPollDAO(orm).getPollRespondents(this).size() + " ";
		}
		return "";
	}

	private String getAudi(ORMInterface orm) {
		int audi = getPollGroupType();
		switch (audi) {
		case POLL_UNIV:
			Universities u = new UniversitiesDAO(orm)
					.getUnivById(getPollGroupId());
			return "university " + u.getTitle();
		case POLL_DEPT:
			Departments d = new DepartmentsDAO(orm)
					.getDepartmentByPk(getPollGroupId());
			return "department " + d.getTitle();
		case POLL_COURSE:
			CourseInstances c = new CourseInstancesDAO(orm)
					.getCourseInstanceById(getPollGroupId());
			return "course " + c.getTitle();
		case POLL_GROUP:
			Groups g = new GroupsDAO(orm).getGroupById(getPollGroupId());
			return "group " + g.getTitle();
		case POLL_BATCH:
			Batches b = new BatchDAO(orm).getBatchesById(getPollGroupId());
			return "batch " + b.getTitle();
		case POLL_INDI:
			return "individual users";
		}
		return "";
	}

	public Poll() {
		setStatus(POLL_STATUS_DRAFT);
	}

	public int compareTo(IDateComparable arg0) {
		if (getDate().getTime() < arg0.getDate().getTime())
			return -1;
		else if (arg0.getDate().getTime() == getDate().getTime())
			return 0;
		else
			return 1;
	}

	public Date getDate() {
		return getPollEndDate();
	}
	
	public String getStatusString(){
		switch(getStatus()){
		case POLL_STATUS_DRAFT:
			return "Draft";
		case POLL_STATUS_POLLING:
			return "Open";
		case POLL_STATUS_CLOSED:
			return "Closed";
		}
		return "";
	}
	
	
	/**
	 * Returns list of users based on their status against the poll.
	 * The userPollStatus is a constant from UserPoll class
	 * @param UserPoll.status
	 * @return list of users of that status
	 */
	public List<Users> getVotersByStatus(int userPollStatus){
		List<UserPoll> upl = this.getUserPoll();
		List<Users> users = new ArrayList<Users>();
		for(UserPoll up:upl){
			if(up.getStatus() == userPollStatus)
				users.add(up.getRespondent());
		}
		return users;
	}
}
